package com.blue.gateway.config.filters.global;

import com.blue.gateway.bean.constants.GatewayHeaderKey;
import com.blue.gateway.config.filters.decorators.LoginSucceedRespDecorator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 4.登录修饰过滤器
 *
 * @author liulei
 * @version 1.0
 */
@Slf4j
@Configuration
public class LoginDecoratorFilter extends AbstractGatewayFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        log.info("4.过滤器链-登录token修饰，对接口的返回值进行修饰组装");
        if (isLoginUrl(exchange)) {
            String scope = exchange.getRequest().getHeaders().getFirst(GatewayHeaderKey.SCOPE);
            ServerHttpResponseDecorator decorator = new LoginSucceedRespDecorator(exchange.getResponse(), Integer.valueOf(scope));
            return chain.filter(exchange.mutate().response(decorator).build());
        }
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return -70;
    }


}
